package com.elite;

import java.io.IOException;
import java.sql.Connection;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPFile;

public class FileFTP implements Runnable{
	private static final Log log=LogFactory.getLog(FileFTP.class);
	public static com.elite.FtpBean ftpbean;
	public static String ip;
	public static String port;
	public static String username;
	public static String password;
	public static String dotime;
	public static String filename;
	public static String sqlname;
	public static String sqlname1;
	public static String result;
	private static String filename1="";
	private static FTPFile[] ff=null;
	private static String sql="";
	private static List sql_list=null;
	private static String name1="";
	private static String judgement="";
	private boolean flag = true; //停止线程标记
	private int judgeNum=0;
	private static String url="";
	private static String urljudge="";
	public com.elite.FtpBean getFtpbean() {
		return ftpbean;
	}

	public void setFtpbean(com.elite.FtpBean ftpbean) {
		this.ftpbean = ftpbean;
	}
	
	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public String getPort() {
		return port;
	}

	public void setPort(String port) {
		this.port = port;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getDotime() {
		return dotime;
	}

	public void setDotime(String dotime) {
		this.dotime = dotime;
	}

	public String getFilename() {
		return filename;
	}

	public void setFilename(String filename) {
		this.filename = filename;
	}

	public static String getSqlname() {
		return sqlname;
	}

	public static void setSqlname(String sqlname) {
		FileFTP.sqlname = sqlname;
	}

	public static String getResult() {
		return result;
	}

	public static void setResult(String result) {
		FileFTP.result = result;
	}

	public static String getSqlname1() {
		return sqlname1;
	}

	public static void setSqlname1(String sqlname1) {
		FileFTP.sqlname1 = sqlname1;
	}

	public void run(){
		while(flag){
			url="ftp://"+ip+":"+port+"/";
			Date date=new Date();
			String nowtime=String.format("%tR",date); //获取现在几点
			log.info("现在的时间:"+nowtime);
			nowtime=nowtime.substring(0,2);
			log.info("现在几点:"+nowtime);
			log.info("配置文件取到的时间:"+dotime);
			if(nowtime.equals(dotime)&&judgeNum==0){
				try{
					doFTP();
					judgeNum=0;
				}catch(Exception e){
					log.info("过程错误:"+e);
				}
			}else if(nowtime.equals("24")){
				if(judgeNum==1){
					judgeNum=0;
				}else if(judgeNum==0){
					try{
						doFTP();
					}catch(Exception e){
						log.info("过程错误:"+e);
					}
				}
			}
			try { 
				Thread.sleep(59*60*1000); 
				} 
				catch (Exception e) {
					log.info("错误:"+e);
					} 
		}
	}
	public static FTPClient GetFtp(String host,int port,String username,String password){
		FTPClient ftp;
		ftp = new FTPClient(); 
		try{
			ftp.connect(host, port);
			log.info("host:"+host);
			log.info("port:"+port);
			ftp.setControlEncoding("GBK");
			FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);
			conf.setServerLanguageCode("zh");
			ftp.login(username, password);
			log.info("username:"+username);
			log.info("password:"+password);
		}catch(Exception e){
			log.info("FTP连接失败:"+e);
		}
		return ftp;
		
	}
	
	public static void doFTP() throws IOException{
		FTPClient ftp=GetFtp(ip,Integer.parseInt(port),username,password);
		if(ftp!=null){
			try{
				ff=ftp.listFiles(filename);
				if(ff!=null){
					log.info("文件个数+2:"+ff.length);
					for(int i=2;i<ff.length;i++){
						filename1=ff[i].getName();
						filename1=new String(filename1.getBytes("gbk"), "iso8859-1");
						log.info("入参文件名(带后缀):"+filename1);
						name1=filename1.substring(0, filename1.lastIndexOf("."));
						log.info("入参文件名:"+name1);
						try{
							log.info("---ftpbean.test-----");
							String name2=new String(name1.getBytes("iso8859-1"),"gbk");
							judgement=ftpbean.test(sqlname, name2);
						}catch(Exception e){
							ftp.logout();
							ftp.disconnect();
							log.info("获取ftpbean错误:"+e);
						}
						if(judgement.equals("1")||judgement.equals("0")){
							if(judgement.equals("1")){
								Date date=new Date();
								String dd=String.format("%tF",date); //获取现在几号
								dd=dd.replace("-", "");	
								boolean a=ftp.makeDirectory(dd);
								boolean aa=ftp.rename(filename+"/"+filename1,dd+"/"+filename1);
								if(aa==true){
									filename1=new String(filename1.getBytes("iso8859-1"),"gbk");
									name1=new String(name1.getBytes("iso8859-1"),"gbk");
									String url1=url+dd+"/"+filename1;
									log.info("URL:"+url1);
									urljudge=ftpbean.test1(sqlname1,url1,name1);
									if(urljudge.equals("1")){
										log.info("返回URL成功");
									}else if(urljudge.equals("0")){
										log.info("返回URL失败");
									}
									log.info("成功将文件"+filename1+"转移到"+dd+"文件夹下");
								}else{
									log.info("失败将文件"+filename1+"转移到"+dd+"文件夹下");
								}
								
							}else{
								boolean aa=ftp.rename(filename+"/"+filename1,"Failed/"+filename1);
								filename1=new String(filename1.getBytes("iso8859-1"),"gbk");
								name1=new String(name1.getBytes("iso8859-1"),"gbk");
								log.info("失败转移前:"+filename+"/"+filename1);
								log.info("失败转移后:"+"Failed/"+filename1);
								if(aa==true){
									String url1=url+"Failed/"+filename1;
									log.info("URL:"+url1);
									urljudge=ftpbean.test1(sqlname1,url1,name1);
									if(urljudge.equals("1")){
										log.info("返回URL成功");
									}else if(urljudge.equals("0")){
										log.info("返回URL失败");
									}									
									log.info("成功将文件"+filename1+"转移到Failed文件夹下");
								}else{
									log.info("失败将文件"+filename1+"转移到Failed文件夹下");
								}
							}
						}else{
							ftp.logout();
							ftp.disconnect();
							log.info("查询无返回");
						}
					}
				}else{
					ftp.logout();
					ftp.disconnect();
					log.info("没有获得"+filename+"路径下文件目录");
				}
			}catch(Exception e){
				ftp.logout();
				ftp.disconnect();
				log.info("扫描"+filename+"路径下文件夹错误:"+e);
			}
		
		}else{
			ftp.logout();
			ftp.disconnect();
			log.info("没有获得FTP连接对象");
		}	
		ftp.logout();
		ftp.disconnect();
	}
}





















